package com.yuan.util.bean;

import com.yuan.util.date.DateUtil;
import com.yuan.util.string.StrUtil;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 类描述:  	[Bean和Map转换工具类]
 * 创建人:  	[YouYuan]
 * 创建时间:	[2017-5-19 上午10:57:47]
 * 修改人:  	[YouYuan]
 * 修改时间:	[2017-9-27]
 * 版本:    	[v1.1]
 */
public class BeanMapUtil {

    /**
     * 将Map转换成指定的Bean
     * @param map
     * @param beanClass
     * @return
     */
    public static Object mapToBean(Map map, Class beanClass) {
        Object inst = null;
        try {
            Field[] fields = beanClass.getDeclaredFields();

            Constructor<?> constructor1 = beanClass.getConstructor();
            inst = constructor1.newInstance();

            for (Field field : fields) {
                if (field.getName().equals("serialVersionUID")) {
                    continue;
                }
                try {
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), inst.getClass());
                    Method method = pd.getWriteMethod();
                    Object value = map.get(field.getName());
                    if (null != value) {
                        Class<?> type = field.getType();
                        String simpleName = type.getSimpleName();
                        if (StrUtil.strIn(simpleName, "Long", "long")) {
                            method.invoke(inst, StrUtil.isEmpty(value.toString()) ? null : Long.parseLong(value.toString()));
                        } else if ("Date".equals(simpleName)) {
                            if (value != null) {
                                if ("java.sql.Date".equals(type)) {
                                    if (value instanceof java.sql.Date) {
                                        method.invoke(inst, value);
                                    } else {
                                        method.invoke(inst, StrUtil.isEmpty(value.toString()) ? null : DateUtil.convertToSqlDate(value.toString()));
                                    }
                                } else {
                                    if (value instanceof java.util.Date) {
                                        method.invoke(inst, value);
                                    } else {
                                        method.invoke(inst, StrUtil.isEmpty(value.toString()) ? null : DateUtil.convertToDate(value.toString()));
                                    }
                                }
                            }
                        } else if (StrUtil.strInIgnoreCase(simpleName, "Integer", "int")) {
                            method.invoke(inst, StrUtil.isEmpty(value.toString()) ? null : Integer.parseInt(value.toString()));
                        } else if (StrUtil.strInIgnoreCase(simpleName, "Double", "double")) {
                            method.invoke(inst, StrUtil.isEmpty(value.toString()) ? null : Double.parseDouble(value.toString()));
                        } else if (StrUtil.strInIgnoreCase(simpleName, "Float", "float")) {
                            method.invoke(inst, StrUtil.isEmpty(value.toString()) ? null : Float.parseFloat(value.toString()));
                        } else if ("List".equalsIgnoreCase(simpleName)) {
                            method.invoke(inst, null == value ? null : (List) value);
                        } else if ("Map".equalsIgnoreCase(simpleName)) {
                            method.invoke(inst, null == value ? null : (Map) value);
                        } else {
                            method.invoke(inst, value);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inst;
    }

    /**
     * 将bean对象转换成Map
     * @param beanObj
     * @return
     */
    public static Map beanToMap(Object beanObj) {
        Map map = new HashMap();
        if (null == beanObj) {
            return map;
        }
        try {
            Class<?> classType = beanObj.getClass();
            Field[] fields = classType.getDeclaredFields();

            for (Field field : fields) {
                if (field.getName().equals("serialVersionUID")) {
                    continue;
                }
                try {
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
                    Method method = pd.getReadMethod();
                    Object value = method.invoke(beanObj);
                    if (null != value) {
                        map.put(field.getName(), value);
                    } else {
                        if ("Long".equals(field.getType().getSimpleName()) || "Integer".equals(field.getType().getSimpleName())) {
                            map.put(field.getName(), null);
                        } else {
                            map.put(field.getName(), "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 将Bean列表转换成Map列表
     * @param beans
     * @return
     */
    public static List<Map> beanListToMapList(List beans) {
        List<Map> listMap = new ArrayList<Map>();
        if (beans == null) {
            return listMap;
        }
        for (Object bean : beans) {
            Map beanToMap = beanToMap(bean);
            listMap.add(beanToMap);
        }
        return listMap;
    }

    /**
     * 将Map列表转换成Bean列表
     * @param mapList
     * @param beanClass
     * @return
     */
    public static List mapListToBeanList(List<Map> mapList, Class beanClass) {
        List beanList = new ArrayList();
        if (mapList == null) {
            return beanList;
        }
        for (Map map : mapList) {
            Object mapToBean = mapToBean(map, beanClass);
            beanList.add(mapToBean);
        }
        return beanList;
    }
}
